<template>
  <div class="container">
    <div class="layout-card" style="min-height: auto">
      <div class="card-content card-search">
        <div class="content-bar">
          <div class="title">
            <slot name="title">数据筛选</slot>
          </div>
        </div>
        <FilterBar
          ref="FilterRef"
          :close-collapse="true"
          :filters="exampleOptions1"
          :hasButton="true"
          @search-change="onSearch"
          @search-reset="onReset"
        />
      </div>
    </div>
    <div style="display: flex">

      <div style="min-width:400px !important;height: 667px;background-color: #ffffff;margin-right: 15px;border-radius: 8px">
        <div class="companyTree">
          <div class="title">
            <div>组织架构及分类信息
            </div>

          </div>
          <div class="tree">
            <el-scrollbar>
              <el-tree
                ref="treeRefs"
                :current-node-key="currentNodeKey"
                class="filter-tree"
                :data="organizeTree"
                :props="defaultProps"
                default-expand-all
                :expand-on-click-node="false"
                node-key="id"
                highlight-current
                v-loading="loadingTree"
                style="max-height: 600px;overflow: auto;"
              >
                <template #default="{ node, data }">

                <span class="custom-tree-node" @click="handleNodeClick(data)">
                  <span class="custom-tree-node-left" >
                    <span class="custom-tree-node-left-label" :title="node.label">
                     {{node.label}}
                    </span>
                  </span>
                  <span class="oper">
                    <el-icon
                      class="el-input__icon"
                      color="#427CC9"
                      @click="e => addEvent(data, e)"
                      v-if="(data.isCatalogue==-1||data.isCatalogue==0)&&data.level!=4"
                    >
                      <CirclePlus/>
                    </el-icon>
                    <el-icon
                      class="el-input__icon"
                      color="#427CC9"
                      @click="editEvent(data, node)" v-if="data.isCatalogue!=-1&&data.isCatalogue!=-2"
                    >
                      <EditPen/>
                    </el-icon>
                    <el-popconfirm
                      title="是否删除?"
                      confirmButtonText="确定"
                      cancelButtonText="取消"
                      @confirm="() => deleteTypeFn(data, node)"
                    >
                      <template #reference>
                        <el-icon
                          class="el-input__icon"
                          color="#e13e53"
                          v-if="data.isCatalogue!=-1&&data.isCatalogue!=-2">
                          <Delete/>
                        </el-icon>
                      </template>
                    </el-popconfirm>

                  </span>
                </span>
                </template>
              </el-tree>
            </el-scrollbar>
          </div>
        </div>
      </div>
      <div style="width: 100%; height: 667px;">
        <div class="layout-card">
          <div class="card-content" style="margin: 0;">
            <div class="content-bar">
              <div class="title">
                <!-- 列表标题 #title -->
                <slot name="content-title">工作标准</slot>
              </div>
              <div>
                <el-button type="primary" @click="handleCreate">新建标准</el-button>
                <el-button type="primary" @click="handleEdit"  :disabled="standardsData.length>0?false:true" >编辑</el-button>
              </div>
            </div>
            <div class="card-main">
              <table-common :loading="loading" @sort-change="handleSortChange" :columns="exampleColumns"
                            :pagination="pager"
                            :table-data="standardsData" size="small" @current-change="currentChange"
                            @size-change="sizeChange" stripe :height="'491px'">

              </table-common>
            </div>
          </div>
        </div>
      </div>
    </div>
    <edit-dialog :centerDialogVisible="centerDialogVisible" :tilte="addTilte" :key="timer" :defaultButton=false
                 :width="'80%'" :closeOnClickModal=false @Cancel="cancel"
    >
      <template #content>
        <el-steps :active="activeStep" align-center>
          <el-step title="选择文件" style="width: 100%"/>

          <el-step title="提取操作规程"/>
          <el-step title="操作规程填写规则"/>
        </el-steps>
        <div class="step-one" v-show="activeStep==0">
          <div class="border">
            <el-form
              ref="ruleFormRef"
              label-width="120px"
              class="demo-ruleForm"
              status-icon
              @submit.native.prevent
            >
              <el-row :gutter="24">
                <el-col :span="6">


                  <el-form-item label="选择标准来源" prop="name">

                    <el-select v-model="sourceval" class="filter-input" clearable
                               placeholder="请选择">
                      <el-option
                        v-for="item in selectData"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="24" v-show="sourceval==0">
                  <el-upload class="upload-demo" ref="elUploadFile" :on-change="handleChangeFile" :on-exceed="handleExceed" action="#" :limit="1" :auto-upload="false" style="width: 255px;">
                    <!-- <el-button type="primary">Click to upload</el-button> -->
                    <button>选择文件</button>&nbsp;&nbsp;
                    <template #tip>
                      <span>{{ file?'已选择文件':'未选择任何文件'}}</span>
                    </template>
                  </el-upload>
                </el-col>
                <el-col :span="24" v-show="sourceval==1" style="height:265px;overflow: hidden;">
                  <div style="display: flex">
                    <div style=" min-width:450px !important;">
                      <div class="companyTree">
                        <div class="tree">
                          <el-scrollbar>
                            <el-tree
                              ref="treeRef"
                              class="filter-tree"
                              :data="companyData"
                              :props="defaultProps"
                              :expand-on-click-node="false"
                              @node-click="handleNodeClick1"
                              highlight-current
                              default-expand-all
                              style="max-height: 250px;overflow: auto;"
                            >
                              <template #default="{ node, data }">
                                <span class="custom-tree-node">
                                  <span class="custom-tree-node-left">
                                    <span class="custom-tree-node-left-label" :title="node.label">
                                      {{ node.label }}
                                    </span>
                                  </span>
                                </span>
                              </template>
                            </el-tree>
                          </el-scrollbar>
                        </div>
                      </div>
                    </div>
                    <div style="float: left; width: 72%; max-height:250px;overflow-y: auto">
                      <layout-card
                        headline="数据列表"
                        :hasButton="false"
                      >
                        <el-table
                          ref="accountRef"
                          :data="tabeData2"
                          highlight-current-row
                          style="width: 100%"

                          @select="selectRow"
                        >
                          <el-table-column type="selection" width="55" />
                          <el-table-column property="enclosureName" label="文件名称"/>
                          <el-table-column property="code" label="文件编号"/>
                          <el-table-column property="enclosureSize" label="文件大小"/>
                          <el-table-column property="enclosureType" label="文件类型"/>
                          <el-table-column property="enclosureVersion" label="文件版本"/>
                        </el-table>
                        <TablePagination
                          :pagination="pager1"
                          @current-change="currentChange1"
                          @size-change="sizeChange1"
                        />
                      </layout-card>
                    </div>
                  </div>

                </el-col>
              </el-row>

            </el-form>
          </div>
        </div>
        <div v-if="activeStep==1">
          <el-table ref="tableRef" :data="dataList" style="min-width: 100%;height:500px;">
            <el-table-column prop="index" type="index" label="序号" width="80"/>
            <el-table-column prop="content" label="操作流程" header-align="center">
              <template #default="scope">

                <div :style="{ color: scope.row.content=='暂无数据' ? '#ccc' : '#000' }">
                  {{ scope.row.content }}
                </div>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div v-show="activeStep==2">
          <!--更改序号为自定义Id，上移下移下标更改为自定义Id-->
          <el-table ref="tableRef" :data="dataList2" style="width: 100%" max-height="580">
            <el-table-column fixed  prop="indexId" label="序号" width="80">
              <template #default="scope">
                {{ scope.row.indexId + 1 }}
              </template>
            </el-table-column>
            <el-table-column prop="content" label="操作流程" header-align="center">
              <template #default="scope">

                <div :style="{ color: scope.row.content=='暂无数据' ? '#ccc' : '#000' }">
                  {{ scope.row.content }}
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="commendPenalize" label="奖惩方式"  v-if="flag0===1">
              <template #header>
                <div style="display: flex;align-items:center;justify-content: center">
                  <span style="margin-right: 10px">奖惩方式</span>
                </div>
              </template>
              <template #default="scope">

                <el-select size="mini" clearable v-model="scope.row.commendPenalize"
                           placeholder="请选择">
                  <el-option v-for="item in selectData1" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column prop="score" label="分值"  v-if="flag0===1" width="180">
              <template #header>
                <div style="display: flex;align-items:center;justify-content: center">
                  <span style="margin-right: 10px">分值</span>
                </div>
              </template>
              <template #default="scope">

                <el-input-number size="mini" min="0" max="99" v-model="scope.row.score"  @input="validatInput"  :precision="0"/>
              </template>
            </el-table-column>
            <el-table-column prop="ruleOptions" label="下拉选项"  v-if="flag1===1">
              <template #header>
                <div style="display: flex;align-items:center;justify-content: center">
                  <span style="margin-right: 10px">下拉选项</span>
                </div>
              </template>
              <template #default="scope">
                <el-select size="mini" clearable v-model="scope.row.ruleOptions"
                           placeholder="请选择" @change="onRuleOptionsChange(scope.row)">
                  <el-option v-for="item in selectData2" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column prop="ruleValue" label="默认值" v-if="flag1===1" >
              <template #header>
                <div style="display: flex;align-items:center;justify-content: center">
                  <span style="margin-right: 10px">默认值</span>
                </div>
              </template>

              <template #default="scope">
                <el-select size="mini" clearable v-model="scope.row.ruleValue"
                           placeholder="请选择">
                  <el-option v-for="(item,index) in scope.row.rules" :key="index" :label="item.label"
                             :value="item.value">
                  </el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column align="right"  fixed="right">
              <template #header>

                <el-dropdown trigger="click" size="mini">
                  <div style="display: flex;align-items:center">
                    <span style="margin-right: 10px">新增列</span>
                    <el-icon
                      class="el-input__icon"
                      color="#427CC9"
                    >
                      <CirclePlus/>
                    </el-icon>
                  </div>
                  <template #dropdown>
                    <el-dropdown-menu>
                      <div style="padding: 0 10px">
                        <el-checkbox-group size="mini"
                                           v-model="isCheckIdList"
                                           style="display: flex;flex-flow: column"
                        >
                          <el-checkbox v-for="item in columData" :key="item.value" :label="item.value" size="mini">
                            {{item.label }}
                          </el-checkbox>
                        </el-checkbox-group>
                      </div>
                    </el-dropdown-menu>
                  </template>
                </el-dropdown>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="180" fixed="right">
              <template #default="scope">
                <el-button link type="primary" size="small" @click="moveUp1(scope.$index,scope.row)"
                           v-if="scope.row.indexId!==0">上移
                </el-button>
                <el-button link type="primary" size="small" @click="moveDown1(scope.$index,scope.row)"
                           v-if="scope.row.indexId<(dataList.length-1)">下移
                </el-button>
                <el-button link type="primary" size="small" @click="remove1(scope.$index,scope.row)">移除</el-button>
              </template>
            </el-table-column>

          </el-table>
          <!--新增页码组件-->
          <TablePagination
            :pagination="pagerMore"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
          />
        </div>
      </template>
      <template #custom>
        <span class="dialog-footer">
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="prev" v-if="activeStep>0">上一步</el-button>
          <el-button type="primary" @click="next"  :loading="showLoading" :disabled="showLoading"
                     v-if="activeStep<2">下一步</el-button>
          <el-button type="primary" @click="submit" v-if="activeStep==2" :loading="showLoading">完成</el-button>
        </span>

      </template>
    </edit-dialog>
    <edit-dialog :centerDialogVisible="centerDialogVisible2" :tilte="addTilte" :key="timer" :defaultButton=false
                 :width="'80%'" :closeOnClickModal=false @Cancel="cancel"
    >
      <template #content>
        <!-- 更改序号为自定义Id，上移下移下标更改为自定义Id-->
        <el-table ref="tableRef" :data="tableData2"   max-height="580" style="min-width: 100%;">
          <el-table-column prop="index" fixed  type="index" label="序号" width="80">
            <template #default="scope">
              {{ scope.row.tableRowId + 1 }}
            </template>
          </el-table-column>
          <el-table-column prop="ruleContent" label="操作流程" header-align="center"/>
          <el-table-column prop="commendPenalize" label="奖惩方式"  v-if="flag0===1">
            <template #header>
              <div style="display: flex;align-items:center;justify-content: center">
                <span style="margin-right: 10px">奖惩方式</span>
              </div>
            </template>
            <template #default="scope">

              <el-select size="mini" clearable v-model="scope.row.commendPenalize"
                         placeholder="请选择">
                <el-option v-for="item in selectData1" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="score" label="分值"  v-if="flag0===1" width="180">
            <template #header>
              <div style="display: flex;align-items:center;justify-content: center">
                <span style="margin-right: 10px">分值</span>
              </div>
            </template>
            <template #default="scope">

              <el-input-number size="mini" min="0" max="99" v-model="scope.row.score"  :precision="0"/>
            </template>
          </el-table-column>
          <el-table-column prop="ruleOptions" label="下拉选项"  v-if="flag1===1" header-align="center">
            <template #header>
              <div style="display: flex;align-items:center;justify-content: center">
                <span style=" margin-right: 10px">下拉选项</span>
              </div>
            </template>
            <template #default="scope">
              <el-select size="mini" clearable v-model="scope.row.ruleOptions"
                         placeholder="请选择" @change="onRuleOptionsChange(scope.row)">
                <el-option v-for="item in selectData2" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="ruleValue" label="默认值"  v-if="flag1===1">
            <template #header>
              <div style="display: flex;align-items:center;justify-content: center">
                <span style="margin-right: 10px">默认值</span>
              </div>
            </template>
            <template #default="scope">
              <el-select size="mini" clearable v-model="scope.row.ruleValue"
                         placeholder="请选择">
                <el-option v-for="(item,index) in scope.row.rules" :key="index" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column align="right" width="120" fixed="right">
            <template #header>

              <el-dropdown trigger="click" size="mini">
                <div style="display: flex;align-items:center;justify-content: center">
                  <span style="margin-right: 10px">新增列</span>
                  <el-icon
                    class="el-input__icon"
                    color="#427CC9"
                  >
                    <CirclePlus/>
                  </el-icon>
                </div>
                <template #dropdown>
                  <el-dropdown-menu>
                    <div style="padding: 0 10px">
                      <el-checkbox-group size="mini"
                                         v-model="isCheckIdList"
                                         style="display: flex;flex-flow: column"
                      >
                        <el-checkbox v-for="item in columData" :key="item.value" :label="item.value" size="mini">
                          {{item.label }}
                        </el-checkbox>
                      </el-checkbox-group>
                    </div>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="180" fixed="right">
            <template #default="scope">
              <el-button link type="primary" size="small" @click="moveUp(scope.$index,scope.row)"
                         v-if="scope.row.tableRowId!==0">上移
              </el-button>
              <el-button link type="primary" size="small" @click="moveDown(scope.$index,scope.row)"
                         v-if="scope.row.tableRowId<(tableData1.length-1)">下移
              </el-button>
              <el-button link type="primary" size="small" @click="remove(scope.$index,scope.row)">移除</el-button>
            </template>
          </el-table-column>

        </el-table>
        <!--新增页码组件-->
        <TablePagination
          :pagination="pagerTable"
          @current-change="handleCurrentTable"
          @size-change="handleSizeTable"
        />
      </template>
      <template #custom>
        <span class="dialog-footer">
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="submit1">保存</el-button>
        </span>

      </template>
    </edit-dialog>
    <!-- 编辑分类 -->
    <EditClass :visible="centerDialogVisible1" :companyData="companyData" :isAdd="isAdd" :info="info"
               @cancel="cancel" @ok="handleOk"/>

  </div>
</template>

<script lang="ts" setup>
  import {computed, ref, onBeforeMount, nextTick, reactive, watch} from 'vue'
  import LayoutCard from '@/components/LayoutCard/index.vue'
  import TableCommon from '@/components/TableCommon/index.vue'
  import FilterBar from '@/components/FilterBar/index.vue'
  import {selectData, columData, selectData1, selectData2} from './config/baseData'
  import treeList from '@/components/TreeList/index.vue'
  import editDialog from '@/components/editDialog/index.vue'
  import {image} from '@/assets/index'
  import EditClass from './editClass.vue'
  import {exampleColumns, exampleColumns1, exampleColumns2} from './config/columns'
  import TablePagination from '@/components/TablePagination/index.vue'
  import {useStore} from "vuex";
  import { genFileId  } from "element-plus";
  import {ElMessage, UploadProps, UploadRawFile } from "element-plus";
  import {
    standardClassPage,
    standardClassDel,
    standardList,
    standardList1,
    standardUpdate,
    standardDel,
    uploadFile,
    standardAdd,
    getFileCOntent
  } from '@/api/knowledge/standards'
  import {getChildrenDirectory, getChildrenDirectory1} from "@/api/knowledge/space"

  const defaultProps = {
    children: 'children',
    label: 'name',
  }
  import {
    exampleOptions1,
    dialogOptions,
  } from './config/filters'
  import {enclosurePageList} from "@/api/knowledge/enclosure";

  const accountRef = ref(null)
  const pager = reactive({
    currentPage: 1,
    pageSize: 10,
    total: 0,
    pageCount: 0,
  });
  const pager1 = reactive({
    currentPage: 1,
    pageSize: 10,
    total: 0,
    pageCount: 0,
  });
  const searchModel = ref({
    condition: {
      standardsId: 0,
      ruleContent: undefined,
      startTime: undefined,
      endTime: undefined,
      order: [{
        column: "sor_num",
        method: "asc", // 倒叙
      },
      ],
    },

  })
  const isCatalogueFlag = ref(-1)
  const dataList = ref([])
  //工作标准数据源
  const standardsData = ref([])
  const sourceval = ref()
  //知识库文件集合
  const companyData = ref<any>([])
  const tabeData2 = ref([])
  watch(sourceval, (val) => {
    if (val == 1) {
      //从知识库获取标准
      console.log(2222)
      pager1.total=0;
      pager1.pageSize=10
      pager1.currentPage=1
      companyDataFn()

    }
  })
  const companyDataFn = async () => {
    companyData.value=[]
    companyData.value=JSON.parse(JSON.stringify(organizes.value))
    ergodicTree1(companyData.value)
    // organizes.value.forEach((res: any) => {
    //   const item = {
    //     id: res.organizationId,
    //     name: res.organizationName,
    //     catalogueId: res.organizationId,
    //     children: []
    //   }
    //   companyData.value.push(item)
    //   onGetSpaceList(item.id)
    // })
  };
  const ergodicTree1=(datas:any)=>{
    datas.forEach((item:any)=>{
      if(item.children&&item.children.length>0){
        item.name=item.organizationName
        ergodicTree1(item.children)
      }else{
        item.id=item.organizationId
        item.name=item.organizationName
        item.catalogueId=item.organizationId
        item.children=[]
        onGetSpaceList(item)
      }
    })
  };
  const onGetSpaceList= async (data: any) => {
    const res = await getChildrenDirectory1(data.id,1)
    if (res?.code == '1') {
      if (res.data.length > 0) {
        res.data.forEach((item: any) => {

          if (item.catalogueId == data.catalogueId) {
            data.children.push(item)
          }

        })
        console.log(companyData.value)

      }
    }
  }
  const enclosure = ref()
  const handleNodeClick1 = (row: any) => {
    console.log(row)
    enclosure.value = row
    getEnclosure()
  }
  const getEnclosure = async () => {
    loading.value = true;
    const payload = {
      pageNum: pager1.currentPage,
      pageSize: pager1.pageSize,
      condition: {
        enclosureClassification: 0,
        directoryCode: enclosure.value.id,
        enclosureTypes:["xlsx","xls"]
      }
    };
    const res = await enclosurePageList(payload)
    if (res?.code == '1') {
      tabeData2.value = res.data.list
      pager1.total = res.data.total;
      pager1.pageCount =
        Math.ceil(pager1.total / pager1.pageSize) === 0
          ? 1
          : Math.ceil(pager1.total / pager1.pageSize);
    }
    loading.value = false;
  }
  const selectTable = ref([])
  const selectRow = (list, row) => {
    if (list.length === 0) {
      selectTable.value = [];
      return
    }
    accountRef.value.clearSelection()
    tabeData2.value.forEach((ele) => {
      if (ele.id === row.id) {
        accountRef.value.toggleRowSelection(ele, true)
        selectTable.value = [ele];
      }
    })

  }
  const loading = ref(false)
  const isCheckIdList = ref<any>([])
  const flag0 = ref(-1)
  const flag1 = ref(-1)
  watch(isCheckIdList, (val) => {
    flag0.value = val.includes(0) ? 1 : -1
    flag1.value = val.includes(1) ? 1 : -1

  })
  const tableData1 = ref([]);
  //  拿到vuex里面的登录用户信息
  const {getters} = useStore()
  const organizes1 = computed(() => getters['user/getOrganTree'])
  const organizeTree = ref([])
  const organizes=ref(JSON.parse(JSON.stringify(organizes1.value)))
  const isAdd = ref(false)
  const info = ref(null)
  const centerDialogVisible1 = ref(false)
  //新增分类
  const addEvent =  (data: any, e: Event) => {
    e.stopPropagation()
    e.preventDefault()
      info.value = data;
      centerDialogVisible1.value = true;
      isAdd.value = true;
  }
  //编辑分类
  const editEvent = (data: any, node: any) => {
    isAdd.value = false;
    centerDialogVisible1.value = true;
    info.value = data;
  }
  //删除分类
  const deleteTypeFn = async (data: any, node: any) => {
    const res = await standardClassDel(data.id);
    console.log(res)

    if (res?.code === '1') {
      ElMessage.success("删除成功");
      searchModel.value.condition.standardsId = 0;
      isCatalogueFlag.value = -1;
      standardsData.value=[]
      onGetData()
    } else {
      ElMessage.error(res?.message || '操作失败，请重试！');
    }

  }
  const activeStep = ref(0)
  // 新增弹窗是否打开
  const centerDialogVisible = ref(false)
  const centerDialogVisible2 = ref(false)
  // 弹窗标题
  const addTilte = ref('')
  // 弹窗页面类型
  const dialogType = ref()
  // 时间戳，用于确保弹窗页面能加载不同的界面
  const timer = ref()
  //上传文件
  const file = ref()

  // 打开新增标准弹窗
  const handleCreate = () => {
    if (isCatalogueFlag.value != 1) {
      ElMessage.info("请选择左侧组织分类");
      return
    } else {
      addTilte.value = '新建标准'
      timer.value = new Date().getTime()
      dialogType.value = dialogOptions
      centerDialogVisible.value = true
      console.log(info.value)
    }
  }

  //处理新增页数据
  let tableData2 = ref([])
  //定义分页参数
  const pagerTable = reactive({
    currentPage: 1,
    pageSize: 10,
    total: 0,
    pageCount: 0,
  })
  //重置表格数据
  const resettingTableData = () => {
    let {
      currentPage,
      pageSize
    } = pagerTable
    //重新排序
    tableData1.value.forEach((item: any, index: number) => {
      item.tableRowId = index
    })
    pagerTable.pageCount = Math.ceil(pagerTable.total / pagerTable.pageSize) === 0 ? 1 : Math.ceil(pagerTable.total / pagerTable.pageSize)
    // 数据截取
   // if (currentPage > 1) {
      tableData2.value = tableData1.value.slice((currentPage - 1)*pageSize, pageSize * currentPage)
   // } else {
    //  tableData2.value = tableData1.value.slice(currentPage - 1, pageSize * currentPage)
   // }
  }
  //页码切换
  const handleCurrentTable = (e: number) => {
    pagerTable.currentPage = e
    //  重置数据
    resettingTableData()
  }
  // 每页条数切换
  const handleSizeTable = (size: number) => {
    // 感觉这里要查第一页
    pagerTable.currentPage = 1
    pagerTable.pageSize = size
    //  重置数据
    resettingTableData()
  }
  //编辑标准
  const handleEdit = async () => {
    if (searchModel.value.condition.standardsId === 0) {
      ElMessage.info("请选择左侧组织分类");
      return
    }
    const res = await standardList1(searchModel.value.condition.standardsId)
    if (res?.code == '1') {
      tableData1.value = res.data;
    } else {
      ElMessage.info("该分类没工作标准，请重新选择");
      return
    }

    addTilte.value = '编辑操作流程'
    timer.value = new Date().getTime()
    centerDialogVisible2.value = true
    flag0.value = 0
    flag1.value = 0
    removeData.value = []
    tableData1.value.forEach((item: any) => {
      if (item.commendPenalize != null) {
        flag0.value = 1
      }
      if (item.ruleOptions != null) {
        flag1.value = 1
      }
      item.createTime = undefined
      item.updateTime = undefined
      onGetOptionsData(item)
    })
    pagerTable.currentPage=1
    pagerTable.total = tableData1.value.length
    pagerTable.pageCount = Math.ceil(pagerTable.total / pagerTable.pageSize) === 0 ? 1 : Math.ceil(pagerTable.total / pagerTable.pageSize)
    //  重置数据
    resettingTableData()
    if (flag0.value == 1) {
      isCheckIdList.value[0] = 0
    }
    if (flag1.value == 1) isCheckIdList.value[1] = 1
  }
  //编辑选项回显
  const onGetOptionsData = (row: any) => {
    if (row.ruleOptions == 0) {
      row.rules = [
        {
          label: "符合",
          value: 0
        },
        {
          label: "不符合",
          value: 1
        }
      ]
    } else if (row.ruleOptions == 1) {
      row.rules = [
        {
          label: "正常",
          value: 2
        },
        {
          label: "异常",
          value: 3
        }
      ]
    } else if (row.ruleOptions == 2) {
      row.rules = [
        {
          label: "对",
          value: 4
        },
        {
          label: "错",
          value: 5
        }
      ]
    } else if (row.ruleOptions == 3) {
      row.rules = [
        {
          label: "适用",
          value: 6
        },
        {
          label: "不适用",
          value: 7
        }
      ]
    }
  }
  //选择需要保存的标准数据
  const multipleSelection = ref([])
  //编辑保存标准
  const submit1 = async () => {
    if (tableData1.value.length > 0) {
      const res = await standardUpdate(tableData1.value)
      if (res?.code == '1') {
        if (removeData.value.length > 0) {
          const res1 = await standardDel(removeData.value);
          if (res1?.code == '1') {
            ElMessage.success('更新成功');
            cancel()
            getStandardList()
          } else {
            ElMessage.error(res?.message || '操作失败，请重试！');
          }
        } else {
          ElMessage.success('更新成功');
          cancel()
          getStandardList()
        }

      } else {
        ElMessage.error(res?.message || '操作失败，请重试！');
      }
    } else {
      if (removeData.value.length > 0) {
        const res1 = await standardDel(removeData.value);
        if (res1?.code == '1') {
          ElMessage.success('更新成功');
          getStandardList()
          cancel()
        } else {
          ElMessage.error(res1?.message || '操作失败，请重试！');
        }
      } else {
        ElMessage.success('更新成功');
        cancel()
        getStandardList()
      }
    }
  }
  //选择文件
  const elUploadFile = ref();
  const handleChangeFile: UploadProps['onChange'] = (uploadFile, uploadFiles) => {
    console.log(uploadFile)
    console.log(uploadFiles)
    file.value = uploadFile
  }
  const handleExceed: UploadProps['onExceed'] = (files) => {
    elUploadFile.value!.clearFiles()
    const file = files[0] as UploadRawFile
    file.uid = genFileId()
    elUploadFile.value!.handleStart(file)
  }
  //关闭弹窗
  const cancel = () => {
    sourceval.value = null
    centerDialogVisible.value = false
    centerDialogVisible1.value = false
    centerDialogVisible2.value = false
    file.value = undefined
    tabeData2.value=[]
    multipleSelection.value = []
    activeStep.value = 0
    showLoading.value=false
    isCheckIdList.value = []
    // pager.currentPage = 1,
    // pager.pageSize = 10,
    // pager.total = 0,
    // pager.pageCount = 0
  }
  //下一步
  const showLoading=ref(false)

  //处理新增页数据
  let dataList2 = ref([])
  //定义分页参数
  const pagerMore = reactive({
    currentPage: 1,
    pageSize: 10,
    total: 0,
    pageCount: 0,
  })
  // 重置数据
  const resettingData = () => {
    let {
      currentPage,
      pageSize
    } = pagerMore
    pagerMore.pageCount = Math.ceil(pagerMore.total / pagerMore.pageSize) === 0 ? 1 : Math.ceil(pagerMore.total / pagerMore.pageSize)
    //重新排序
    dataList.value.forEach((item: any, index: number) => {
      item.indexId = index
    })
    //截取数据
   // if (currentPage > 1) {
      dataList2.value = dataList.value.slice((currentPage - 1)*pageSize, pageSize * currentPage)
   // } else {
    //  dataList2.value = dataList.value.slice(currentPage - 1, pageSize * currentPage)
   // }
  }
  //页码切换
  const handleCurrentChange = (e: number) => {
    pagerMore.currentPage = e
    resettingData()
  }
  // 每页条数切换
  const handleSizeChange = (size: number) => {
    pagerMore.currentPage = 1
    pagerMore.pageSize = size
    resettingData()
  }
  const next = () => {
    showLoading.value=true
    if (activeStep.value == 0) {
      if (sourceval.value == 0) {
        //本地文件上传
        if (file.value != undefined) {
          const houzui = file.value.name.substring(file.value.name.lastIndexOf(".")+1);
          console.log(houzui)
          if(houzui == 'xls' || houzui == 'xlsx') {
            const form = new FormData();
            form.append("file", file.value.raw)
            uploadFile(form).then(res => {
              if (res.code == '1') {
                dataList.value = res.data
                dataList.value.forEach((res: any) => {
                  if (res.content == null) {
                    res.ruleContent = res.content = '暂无数据'
                  }
                })

                //展示数据处理
               // resettingData()
                activeStep.value++
              } else {
                ElMessage.error(res.message?res.message:`文件上传失败`)
                elUploadFile.value.clearFiles()
              }
            }).finally(e=>{
              showLoading.value=false
            })
          } else {
            ElMessage.info(`仅支持xls或xlsx文件类型`)
            showLoading.value=false
            elUploadFile.value.clearFiles()
            file.value=undefined
            return
          }
        } else {
          ElMessage.info(`请上传文件`)
          showLoading.value=false
        }
      }else{
        // 知识库获取
        if(selectTable.value.length>0){
          const res= getFileCOntent(selectTable.value[0].enclosureCode).then(res=>{
            if (res.code == '1') {
              dataList.value = res.data
              dataList.value.forEach((res: any) => {
                if (res.content == null) {
                  res.ruleContent = res.content = '暂无数据'
                }
              })
              //展示数据处理
             // resettingData()
              activeStep.value++
            } else {
              ElMessage.error(res.message || "文件上传失败");
              showLoading.value=false
            }
          }).finally(e=>{
            showLoading.value=false
          })
        }else{
          ElMessage.info(`请选择知识库文件`)
          showLoading.value=false
        }
      }
    } else if (activeStep.value == 1) {
      // 数据自定义下标
      activeStep.value++
      dataList.value.forEach((resp: any,index:number) => {
        resp.indexId = index
        if (resp.content == null) {
          resp.ruleContent = resp.content = '暂无数据'
        }
        resp.ruleContent = resp.content
        resp.ruleOptions = null
        resp.ruleValue = null
        resp.commendPenalize = null
        resp.score = null
        resp.rules = []
        resp.standardsId = searchModel.value.condition.standardsId;
      })
      // 页码赋值
      pagerMore.currentPage=1
      pagerMore.total = dataList.value.length
      pagerMore.pageCount = Math.ceil(pagerMore.total / pagerMore.pageSize) === 0 ? 1 : Math.ceil(pagerMore.total / pagerMore.pageSize)
      //展示数据处理
      resettingData()
      showLoading.value=false
    }
  }
  //上一步
  const prev = () => {
    activeStep.value--
    if(activeStep.value==0&&selectTable.value.length+0){
      accountRef.value!.toggleRowSelection(selectTable.value[0], true)
    }
  }
  //提交数据
  const submit = async () => {
    showLoading.value = true;
    dataList.value.forEach(element => {
      delete element.id
    });
    multipleSelection.value = dataList.value;
    // 过滤空数据
    multipleSelection.value = multipleSelection.value.filter(function(e){
      return e.content != '暂无数据'
    })
    const res = await standardAdd(multipleSelection.value)
    if (res?.code === '1') {
      showLoading.value = false;
      ElMessage.success('新增成功！');
      cancel()
      getStandardList()
    } else {
      showLoading.value = false;
      ElMessage.error(res?.message || '操作失败，请重试！');
    }
  }
  const handleCommand = (value: any) => {
    console.log(value)
  }
  //上移
  const moveUp = (index: number, row: any) => {
    console.log(index)
    if (row.tableRowId > 0) {
      let upDate = tableData1.value[row.tableRowId - 1]
      tableData1.value.splice(row.tableRowId - 1, 1)
      tableData1.value.splice(row.tableRowId, 0, upDate)
      //  重置数据
      resettingTableData()
    } else {
      alert('已经是第一条，不可上移')
    }
  }
  //下移
  const moveDown = (index: number, row: any) => {
    // 保存下一条数据
    let downDate = tableData1.value[row.tableRowId + 1]
    // 删除下一条数据
    tableData1.value.splice(row.tableRowId + 1, 1)
    // 增添被删除的那一条数据
    tableData1.value.splice(row.tableRowId, 0, downDate)
    //  重置数据
    resettingTableData()
  }
  const removeData = ref<any>([])
  //移除
  const remove = (index: number, row: any) => {
    removeData.value.push(tableData1.value[row.tableRowId]?.id)
    tableData1.value.splice(row.tableRowId, 1)
    // 更改页码
    pagerTable.total = tableData1.value.length
    pagerTable.pageCount = Math.ceil(pagerTable.total / pagerTable.pageSize) === 0 ? 1 : Math.ceil(pagerTable.total / pagerTable.pageSize)
    //  重置数据
    resettingTableData()
  }
  //上移
  const moveUp1 = (index: number, row: any) => {
    if (row.indexId > 0) {
      let upDate = dataList.value[row.indexId - 1]
      dataList.value.splice(row.indexId - 1, 1)
      dataList.value.splice(row.indexId, 0, upDate)
      //重置数据
      resettingData()
    } else {
      alert('已经是第一条，不可上移')
    }
  }
  //下移
  const moveDown1 = (index: number, row: any) => {
    // 保存下一条数据
    let downDate = dataList.value[row.indexId + 1]
    // 删除下一条数据
    dataList.value.splice(row.indexId + 1, 1)
    // 增添被删除的那一条数据
    dataList.value.splice(row.indexId, 0, downDate)
    //重置数据
    resettingData()
  }
  const remove1 = (index: number, row: any) => {
    dataList.value.splice(row.indexId, 1)
    //更改页码
    pagerMore.total = dataList.value.length
    pagerMore.pageCount = Math.ceil(pagerMore.total / pagerMore.pageSize) === 0 ? 1 : Math.ceil(pagerMore.total / pagerMore.pageSize)
    //重置数据
    resettingData()
  }
  const onRuleOptionsChange = (row: any) => {
    console.log(row)
    row.ruleValue = ''
    onGetOptionsData(row)
  }
  onBeforeMount(() => {
    console.log(organizeTree.value);
    onGetData();
  })
  const loadingTree=ref(false)
  const onGetData =  async () => {
    loadingTree.value=true
    const data= JSON.parse(JSON.stringify(organizes1.value))
    organizeTree.value =[]

    await ergodicTree(data);
    organizeTree.value=data
    loadingTree.value=false
    getTopOne(organizeTree.value[0])
  }
  const ergodicTree=async (datas: any) => {
    for (let item of datas) {
      if (item.children && item.children.length > 0) {
        item.name = item.organizationName
        item.isCatalogue = -2
        item.level = 0
        await ergodicTree(item.children)
      } else {
        item.name = item.organizationName
        item.id = item.organizationId
        item.isCatalogue = -1
        item.children = []
        item.level = 0
        await findTree(item)

      }
    }

  };
  //获取
  const handleOk = () => {
    centerDialogVisible1.value = false
    searchModel.value.condition.standardsId = 0;
    isCatalogueFlag.value = -1;
    standardsData.value=[]
    onGetData();
  }
  //默认选中组织目录第一个
  const currentNodeKey=ref("")
  const treeRefs=ref(null)
  const getTopOne=(data:any)=>{
    if(data.children&&data.children.length>0){
      getTopOne(data.children[0])
    }else{
      nextTick(()=>{
        currentNodeKey.value= data.id
        treeRefs.value.setCurrentKey(currentNodeKey.value)
        console.log(currentNodeKey.value)
        if(data.isCatalogue==1){
          searchModel.value.condition.standardsId = data.id
          isCatalogueFlag.value = data.isCatalogue
          getStandardList()
        }
      })
    }
  }
  //获取组织结构树
  const findTree = (async (data: any) => {
    const res = await standardClassPage(data.organizationId)
    if (res?.code == '1') {
      if (res.data.length > 0) {
        res.data.forEach((item: any) => {
            if (item.catalogueId == data.organizationId) {
              data.children.push(item)
            }
        })
      }
      console.log("-----------")
    }
  })
  /**
   * 点击树形节点查询标准
   */
  const handleNodeClick = (data: any) => {
    console.log(data)
    searchModel.value.condition.standardsId = data.id
    isCatalogueFlag.value = data.isCatalogue
    getStandardList()
  }
  //搜索查询
  const onSearch = (data: any) => {
    console.log(data)
    searchModel.value.condition.ruleContent = data.typeName;
    if (data.lastUpdateTime.length > 0) {
      searchModel.value.condition.startTime = data.lastUpdateTime[0]
      searchModel.value.condition.endTime = data.lastUpdateTime[1]
    }

    getStandardList();
  };
  //重置
  const onReset = (data: any) => {
    console.log(data)
    searchModel.value.condition.ruleContent = undefined
    searchModel.value.condition.startTime = undefined
    searchModel.value.condition.endTime = undefined
    pager.currentPage = 1
    pager.pageSize = 10
    searchModel.value.condition.order = [{
      column: "sor_num",
      method: "desc",
    },]
    getStandardList()
  }
  //排序
  const handleSortChange = (data: any) => {
    console.log(data)
    if (data.order === 'ascending') {
      searchModel.value.condition.order[0].method = "asc"
    } else {
      searchModel.value.condition.order[0].method = "desc"
    }
    getStandardList()
  }
  // 分页
  const sizeChange = (size: number) => {
    // 感觉这里要查第一页？？？？？
    pager.currentPage = 1
    pager.pageSize = size
    searchModel.value.condition.order = [{
      column: "sor_num",
      method: "asc",
    },] // 清空排序？？？
    getStandardList()
  }
  const currentChange = (value: number) => {
    pager.currentPage = value
    getStandardList()
  }
  const currentChange1 = (value: number) => {
    pager1.currentPage = value
    getEnclosure()
  }
  const sizeChange1 = (size: number) => {
    // 感觉这里要查第一页？？？？？
    pager1.currentPage = 1
    pager1.pageSize = size
    getEnclosure()
  }
  const getStandardList = async () => {
    if (searchModel.value.condition.standardsId === 0) {
      ElMessage.info("请选择左侧组织分类");
      return
    }
    const params = {
      pageNum: pager.currentPage,
      pageSize: pager.pageSize,
      ...searchModel.value
    };
    loading.value = true;
    const res = await standardList(params)
    if (res?.code == '1') {
      standardsData.value=[]
      standardsData.value = res.data.list
      pager.total = res.data.total;
      pager.pageCount =
        Math.ceil(pager.total / pager.pageSize) === 0
          ? 1
          : Math.ceil(pager.total / pager.pageSize);

      loading.value = false;
    } else {
      ElMessage.error(res?.message || '操作失败，请重试！');
      loading.value = false;
    }
  }
  const validatInput=(value:any)=>{
    if(value!=null){
      return Number(value.toString().replace(/^(0+)|[^\d]+/g,''))
    }else ""
  }
</script>
<style lang="less" scoped>
  ::-webkit-scrollbar {
    width: 5px;
  }
  ::-webkit-scrollbar-thumb {
    background-color: #dddee0;
  }
  .container {
    background: #f5f7f9;
    min-height: auto;
    overflow: hidden;

    .companyTree {
      padding: 24px;
      margin: 0px 12px 12px 0;
      border-radius: 8px;

      .title {
        font-size: 16px;
        font-family: HarmonyOS_Sans_SC_Medium;
        border-left: #2f7df1 2px solid;
        text-indent: 1em;
        color: #353a42;
        line-height: 16px;
        margin: 10px 0;
        display: flex;
        justify-content: space-between;
      }

      .search {
        margin-bottom: 20px;
      }

      /deep/ .el-tree-node__content {
        height: 32px;

      }

      /deep/ .el-tree-node:focus > .el-tree-node__content {
        background-color: #5daaf0 !important;//背景色
      }

      .custom-tree-node {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: space-between;
        font-size: 14px;
        padding-right: 8px;
        width: 210px;
        // background-color: yellow;

        .custom-tree-node-left {
          max-width: 500px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;

          .custom-tree-node-left-label {
            // width: 400px;
            // overflow: hidden;
            // text-overflow: ellipsis;
            // white-space: nowrap;
            // display: block;
          }
        }

        .oper {
          display: flex;
          justify-content: flex-end;
          align-items: center;
          user-select: none;
          padding-right: 0px;
          width: 100px;

          img {
            width: 20px;
            height: 20px;
            object-fit: contain;

            &:hover {
              transform: scale(1.1);
            }
          }

          img + img {
            margin-left: 8px;
          }

          .border {
            border-radius: 4px;
            border: 1px solid #2f7df1;
            padding: 2px;
            box-sizing: border-box;
            margin-left: 8px;
          }
        }
      }
    }

    .layout-card {
      background: #f5f7f9;
      min-height: 100%;
      overflow: hidden;
      @titleFontFamily: "HarmonyOS_Sans_SC_Medium";

      .card-content {
        padding: 24px;
        margin: 12px 0;
        background: #fff;
        border-radius: 8px;

        &.card-search {
          padding-bottom: 0;
        }

        .content-bar {
          display: flex;
          align-items: center;
          justify-content: space-between;
          margin-bottom: 24px;

          .title {
            font-size: 16px;
            font-family: @titleFontFamily;
            color: #353a42;
            line-height: 24px;
            font-weight: bolder;
          }
        }
      }
    }

    .border {
      border: 1px solid #f5f5f5;
      margin: 24px;
      padding: 24px;
    }

    .drop {
      padding: 10px;
    }

    /deep/ .el-step__description {
      width: 100%;
    }

    .el-input__icon {
      font-size: 20px;
      margin-right: 10px;
    }

  }


</style>
